﻿using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;
using Notes.Model;
using System;
using System.Collections.ObjectModel;

namespace Notes.Helpers
{
    public class SyncHelper
    {
        #region parse data
        
        public static IEnumerable<Note> ParseNotes(string data)
        { 
            XElement Backup = null;
            IEnumerable<Note> result = null;

            Backup = XElement.Parse(data);
            if (Backup != null)
            {
                var notes = Backup.Elements("note");
                result = (from n in notes
                             select new Note
                             {
                                 Id = Convert.ToInt32(n.Element("id").Value),
                                 Title = n.Element("title").Value,
                                 Text = n.Element("text").Value,
                                 Edited = Convert.ToDateTime(n.Element("edited").Value),
                                 CategoryId = Convert.ToInt32(n.Element("category_id").Value),
                                 Deleted = Convert.ToBoolean(n.Element("deleted").Value),
                             });
                result.ToList();
            }
            return result;
        }

        public static IEnumerable<Category> ParseCategories(string data)
        { 
            XElement Backup = null;
            IEnumerable<Category> result = null;

            Backup = XElement.Parse(data);
            if (Backup != null)
            {
                // parse categories
                var categories = Backup.Elements("category");
                result = (from c in categories
                             select new Category
                             {
                                 Id = Convert.ToInt32(c.Element("id").Value),
                                 Name = c.Element("name").Value,
                                 Color = c.Element("color").Value,
                                 Edited = Convert.ToDateTime(c.Element("edited").Value),
                                 Deleted = Convert.ToBoolean(c.Element("deleted").Value),
                             });
                result.ToList();
            }

            return result;
        }

        public static IEnumerable<Photo> ParsePhotos(string data)
        {
            XElement Backup = null;
            IEnumerable<Photo> result = null;

            Backup = XElement.Parse(data);
            if (Backup != null)
            {
                // parse categories
                var photos = Backup.Elements("photo");
                result = (from c in photos
                          select new Photo
                          {
                              Id = Convert.ToInt32(c.Element("id").Value),
                              NoteId = Convert.ToInt32(c.Element("note_id").Value),
                              FileName = c.Element("file_name").Value,
                              Edited = Convert.ToDateTime(c.Element("edited").Value),
                              Deleted = Convert.ToBoolean(c.Element("deleted").Value),
                          });
                result.ToList();
            }

            return result;
        }
        
        #endregion

        #region build backup

        public static XDocument CombineBackupFile(ObservableCollection<Category> Categories, ObservableCollection<Note> Notes, ObservableCollection<Photo> Photos)
        {
            XDocument Backup = new XDocument();
            return Backup;
        }

        #endregion

        #region merge data

        public static ObservableCollection<Category> MergeCategories(ObservableCollection<Category> local, ObservableCollection<Category> remote)
        {
            return null;
        }

        public static ObservableCollection<Note> MergeNotes(ObservableCollection<Note> local, ObservableCollection<Note> remote)
        {
            return null;
        }

        public static ObservableCollection<Photo> MergePhotos(ObservableCollection<Photo> local, ObservableCollection<Photo> remote)
        {
            return null;
        }

        #endregion

        public static void UpdateLocalDatabase(ObservableCollection<Category> Categories, ObservableCollection<Note> Notes, ObservableCollection<Photo> Photos)
        { 
            
        }
    }
}
